<?php

namespace Transaccion\Model\Dao;

use Zend\Db\Adapter\Adapter;
use Zend\Db\Sql\Sql;
use Transaccion\Model\Dao\GenericDAO;

class OrganizacionSeccionDAO extends GenericDAO {

    protected $dbAdapter;

    /**
     * Nombre de la Tabla
     */
    protected $tabla = 'organizacion_seccion';
    protected $vista1 = 'organizacion_seccion_por_cedula_y_lapso';
    protected $vista2 = 'secciones_disponibles';
    private static $organizacionSeccionDao;

    /**
     * Configura Adaptador de Base de Datos
     * 
     * @param \Zend\Db\Adapter\Adapter $adapter
     */
    private function __construct(Adapter $adapter) {
        $this->dbAdapter = $adapter;
    }

    /**
     * 
     * @param type $adapter
     * @return instancia de OrganizacionSeccionDao
     * Patron singleton
     */
    public static function getInstance($adapter) {
        if (self::$organizacionSeccionDao == null) {
            self::$organizacionSeccionDao = new OrganizacionSeccionDao($adapter);
        }
        return self::$organizacionSeccionDao;
    }

    public function getRegistro($id) {
        return $this->getRegistroGeneral($id, $this->tabla, $this->dbAdapter);
    }

    public function getOrganizacionSeccionsSegunIdPensum($idPensum) {
        $sql = new Sql($this->dbAdapter);
        $select = $sql->select();
        $select->from($this->tabla);
        $select->where("id_pensum ='" . $idPensum . "'");
        $selectString = $sql->getSqlStringForSqlObject($select);
        $adapter = $this->dbAdapter;
        $results = $adapter->query($selectString, $adapter::QUERY_MODE_EXECUTE);
        return $results;
    }

    public function getOrganizacionSeccionsSegunIdSeccion($idSeccion) {
        $sql = new Sql($this->dbAdapter);
        $select = $sql->select();
        $select->from($this->tabla);
        $select->where("id_seccion ='" . $idSeccion . "'");
        $selectString = $sql->getSqlStringForSqlObject($select);
        $adapter = $this->dbAdapter;
        $results = $adapter->query($selectString, $adapter::QUERY_MODE_EXECUTE);
        return $results;
    }

    public function getOrganizacionSeccionsSegunIdLapso($idLapso) {
        $sql = new Sql($this->dbAdapter);
        $select = $sql->select();
        $select->from($this->tabla);
        $select->where("id_lapso ='" . $idLapso . "'");
        $selectString = $sql->getSqlStringForSqlObject($select);
        $adapter = $this->dbAdapter;
        $results = $adapter->query($selectString, $adapter::QUERY_MODE_EXECUTE);
        return $results;
    }

    public function getOrganizacionSeccionSegunCedulaYLapso($cedula, $lapso) {
        $sql = new Sql($this->dbAdapter);
        $select = $sql->select();
        $select->from($this->vista1);
        $select->where(array("cedula ='" . $cedula . "'", "lapso ='" . $lapso . "'"));
        $selectString = $sql->getSqlStringForSqlObject($select);
        $adapter = $this->dbAdapter;
        $results = $adapter->query($selectString, $adapter::QUERY_MODE_EXECUTE);
        return $results;
    }

    public function getOrganizacionSeccionSegunCedulaLapsoYFase($cedula, $lapso, $fase) {
        $sql = new Sql($this->dbAdapter);
        $select = $sql->select();
        $select->from($this->vista1);
        $select->where(array("cedula ='" . $cedula . "'", "lapso ='" . $lapso . "'", 'id_periodo_fase' => array($fase, 3, 4)));
        $selectString = $sql->getSqlStringForSqlObject($select);
        $adapter = $this->dbAdapter;
        $results = $adapter->query($selectString, $adapter::QUERY_MODE_EXECUTE);
        return $results;
    }
    
    public function getOrganizacionSeccionesDisponiblesSegunPensumLapso($idPensum, $lapso) {
        $sql = new Sql($this->dbAdapter);
        $select = $sql->select();
        $select->from($this->vista2);
        $select->where(array("idpensum ='" . $idPensum . "'", "descripcionlapso ='" . $lapso. "'"));
        $selectString = $sql->getSqlStringForSqlObject($select);
        $adapter = $this->dbAdapter;
        $results = $adapter->query($selectString, $adapter::QUERY_MODE_EXECUTE);
        return $results;
    }
}
